package com.ruoyi.system.service.impl;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.HospitalSubscribeProject;
import com.ruoyi.system.mapper.HospitalSubscribeProjectMapper;
import com.ruoyi.system.service.IHospitalSubscribeMedicalExamTypeService;
import com.ruoyi.system.service.IHospitalSubscribeProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.HospitalSubscribeSetMealMapper;
import com.ruoyi.system.domain.HospitalSubscribeSetMeal;
import com.ruoyi.system.service.IHospitalSubscribeSetMealService;

/**
 * 体检套餐Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-04-26
 */
@Service
public class HospitalSubscribeSetMealServiceImpl implements IHospitalSubscribeSetMealService 
{
    @Autowired
    private HospitalSubscribeSetMealMapper hospitalSubscribeSetMealMapper;

    @Autowired
    private HospitalSubscribeProjectMapper hospitalSubscribeProjectMapper;

    @Autowired
    private IHospitalSubscribeMedicalExamTypeService iHospitalSubscribeMedicalExamTypeService;

    /**
     * 查询体检套餐
     * 
     * @param id 体检套餐主键
     * @return 体检套餐
     */
    @Override
    public HospitalSubscribeSetMeal selectHospitalSubscribeSetMealById(Long id)
    {
        return hospitalSubscribeSetMealMapper.selectHospitalSubscribeSetMealById(id);
    }

    /**
     * 查询体检套餐列表
     * 
     * @param hospitalSubscribeSetMeal 体检套餐
     * @return 体检套餐
     */
    @Override
    public List<HospitalSubscribeSetMeal> selectHospitalSubscribeSetMealList(HospitalSubscribeSetMeal hospitalSubscribeSetMeal)
    {
        List<HospitalSubscribeSetMeal> mealList = hospitalSubscribeSetMealMapper.selectHospitalSubscribeSetMealList(hospitalSubscribeSetMeal);
        for (HospitalSubscribeSetMeal meal : mealList) {
            String projectIds = meal.getProjectIds();
            String[] ids = projectIds.split(",");
            // 检查项
            List<HospitalSubscribeProject> projects = hospitalSubscribeProjectMapper.getHospitalSubscribeProjectByIds(Arrays.stream(ids).collect(Collectors.toList()));
            meal.setProjectList(projects);
            // 检查类型
            meal.setSubscribeMedicalExamType(iHospitalSubscribeMedicalExamTypeService.selectHospitalSubscribeMedicalExamTypeById(meal.getMedicalExamType()));

        }
        return mealList;
    }

    /**
     * 新增体检套餐
     * 
     * @param hospitalSubscribeSetMeal 体检套餐
     * @return 结果
     */
    @Override
    public int insertHospitalSubscribeSetMeal(HospitalSubscribeSetMeal hospitalSubscribeSetMeal)
    {
        hospitalSubscribeSetMeal.setCreateTime(DateUtils.getNowDate());
        return hospitalSubscribeSetMealMapper.insertHospitalSubscribeSetMeal(hospitalSubscribeSetMeal);
    }

    /**
     * 修改体检套餐
     * 
     * @param hospitalSubscribeSetMeal 体检套餐
     * @return 结果
     */
    @Override
    public int updateHospitalSubscribeSetMeal(HospitalSubscribeSetMeal hospitalSubscribeSetMeal)
    {
        hospitalSubscribeSetMeal.setUpdateTime(DateUtils.getNowDate());
        return hospitalSubscribeSetMealMapper.updateHospitalSubscribeSetMeal(hospitalSubscribeSetMeal);
    }

    /**
     * 批量删除体检套餐
     * 
     * @param ids 需要删除的体检套餐主键
     * @return 结果
     */
    @Override
    public int deleteHospitalSubscribeSetMealByIds(Long[] ids)
    {
        return hospitalSubscribeSetMealMapper.deleteHospitalSubscribeSetMealByIds(ids);
    }

    /**
     * 删除体检套餐信息
     * 
     * @param id 体检套餐主键
     * @return 结果
     */
    @Override
    public int deleteHospitalSubscribeSetMealById(Long id)
    {
        return hospitalSubscribeSetMealMapper.deleteHospitalSubscribeSetMealById(id);
    }
}
